# refugees by country or territory of asylum

ref <- wb(indicator = "SM.POP.REFG")

ref_2018 <- ref %>%
  filter(date == 2018) %>%
  select(iso3c, country, value)
# refugees by country or territory of origin

ref_origin <- wb(indicator = "SM.POP.REFG.OR") %>%
  select(iso3c, country, value, date) %>%
  slice(1335:6493)
# data cleaning

gdp_percap_grow <- wb(indicator = "NY.GDP.PCAP.KD.ZG")

gdp_percap <- wb(indicator = "NY.GDP.PCAP.CD")

gdp <- wb(indicator = "NY.GDP.MKTP.CD")

gdp_grow_ref <- merge(gdp_percap_grow, ref, by = c("iso3c", "date", "country", "iso2c")) %>%
  mutate(gdp_percap_growth = value.x,
            refugee_number = value.y) %>%
  select(iso3c, iso2c, country, date, gdp_percap_growth, refugee_number)

with_gdp <- merge(gdp_grow_ref, gdp, by = c("iso3c", "date", "country", "iso2c")) %>%
  mutate(gdp = value) %>%
  select(iso3c, iso2c, country, date, gdp, gdp_percap_growth, refugee_number)

data <- merge(with_gdp, gdp_percap, by = c("iso3c", "date", "country", "iso2c")) %>%
  mutate(gdp_percap = value) %>%
  select(iso3c, iso2c, country, date, gdp, gdp_percap, gdp_percap_growth, refugee_number)

# refugees and gdp per cap growth 
  
  # regression

lm(gdp_percap_growth ~ refugee_number, data = data) %>%
  tidy(conf.int = TRUE, conf.level = 0.90) %>%
  mutate(name = c("Intercept", "Number of Refugees")) %>%
  select(name, estimate, conf.low, conf.high) %>%
  gt() %>%
  tab_header(title = "Refugee Counts and GDP Per Capita Growth") %>%
  cols_label(name = "", estimate = "Estimate", conf.low = "5th Percentile", conf.high = "95th Percentile")
Refugee Counts and GDP Per Capita Growth
Estimate 5th Percentile 95th Percentile
Intercept 2.167062e+00 2.048611e+00 2.285513e+00
Number of Refugees -1.782173e-08 -6.030405e-08 2.466058e-08
  # graph

data %>%
  ggplot(aes(refugee_number, gdp_percap_growth)) +
  geom_point() +
  scale_x_continuous(labels = scales::comma) +
  geom_smooth(method = "lm") +
  labs(title = "Refugee Counts and GDP Per Capita Growth", x = "Number of Refugees", y = "GDP Percapita Growth Rate")

# refugees and gdp percap

lm(gdp_percap ~ refugee_number, data = data) %>%
  tidy(conf.int = TRUE, conf.level = 0.90) %>%
  mutate(name = c("Intercept", "Number of Refugees")) %>%
  select(name, estimate, conf.low, conf.high) %>%
  gt() %>%
  tab_header(title = "Refugee Counts and GDP Per Capita") %>%
  cols_label(name = "", estimate = "Estimate", conf.low = "5th Percentile", conf.high = "95th Percentile")
Refugee Counts and GDP Per Capita
Estimate 5th Percentile 95th Percentile
Intercept 1.095368e+04 1.055900e+04 1.134837e+04
Number of Refugees -7.260857e-04 -8.676401e-04 -5.845313e-04
  # graph

data %>%
  ggplot(aes(gdp_percap, refugee_number)) +
  geom_point() +
  scale_x_continuous(labels = scales::comma) +
  geom_smooth(method = "lm")

# refugees and gdp

lm(gdp ~ refugee_number, data = data) %>%
  tidy(conf.int = TRUE, conf.level = 0.90) %>%
  mutate(name = c("Intercept", "Number of Refugees")) %>%
  select(name, estimate, conf.low, conf.high) %>%
  gt() %>%
  tab_header(title = "Refugee Counts and GDP") %>%
  cols_label(name = "", estimate = "Estimate", conf.low = "5th Percentile", conf.high = "95th Percentile")
Refugee Counts and GDP
Estimate 5th Percentile 95th Percentile
Intercept 674426730695 535359865416 813493595974
Number of Refugees 1389293 1339417 1439169
  # graph

data %>%
  ggplot(aes(refugee_number, gdp)) + 
  geom_point() + 
  scale_x_continuous(labels = scales::comma) +
  scale_y_continuous(labels = scales::comma) +
  geom_smooth(method = "lm", se = FALSE, color = "green")

# refugee asylum location map 2018

plot_geo(data = ref, locationmode = 'country names') %>%
  add_trace(
    z = ~value, locations = ~country, color = ~value, colorbar = list(tick0 = 0,
                                                                      tickmode = "log",
                                                                      dtick =1000000),
    colorscale = list(
      list(0, 'rgb(255,255,204)'),
      list(10/10000000, 'rgb(229,255,204)'),
      list(100/10000000, 'rgb(204,255,229)'),
      list(1000/10000000, 'rgb(102,255,255)'),
      list(10000/10000000, 'rgb(107,178,255)'),
      list(100000/10000000, 'rgb(0,128,255)'),
      list(1000000/10000000, 'rgb(0,0,255)'),
      list(10000000/10000000, 'rgb(153,0,76)')))
# rich countries and refugees
# according to the World Bank, "rich" countries are countries with GDP Per Cap >= $12,376
# https://datahelpdesk.worldbank.org/knowledgebase/articles/906519

data %>%
  filter(country == "North America") %>%
  ggplot(aes(gdp_percap, refugee_number)) +
  geom_point() +
  scale_x_continuous(labels = scales::comma) +
  geom_smooth(method = "lm") +
  labs(title = "Refugees")

#